package main

import (
	"flag"
	"io"
	"os"
	"runtime"
	"shop/config"
	"shop/midware"
	"shop/monitor"
	"shop/service"
	"shop/utils/convertutil"
	"time"

	"github.com/cihub/seelog"
	"github.com/gin-gonic/gin"
)

var startTime = convertutil.FormatStandardDate(time.Now())
var serverPort = "8888"

func main() {
	flag.StringVar(&serverPort, "port", "8888", "default config path")
	flag.Parse()

	config.InitSeelog()      // 加载seelog日志配置
	service.StartTimerTask() // 开启定时任务
	// gin框架日志配置
	file, _ := os.Create("output/gin.log")
	gin.DefaultWriter = io.MultiWriter(file, os.Stdout)

	if config.IsDevEnv() {
		gin.SetMode(gin.TestMode)
	}
	engine := gin.Default()
	// 全局中间件
	engine.Use(midware.Recover)
	engine.Use(midware.Cors)                                                 // 跨域
	engine.Use(midware.IpLimiter(runtime.NumCPU()*10, runtime.NumCPU()*100)) // 对于所有ip，限流所有接口

	// 路由设置
	monitor.Init() // 初始化监控
	defer monitor.StopPrometheus()
	setRouters(engine)
	setAdminRouters(engine)

	seelog.Infof("当前是否是生产环境: %v, 使用端口: %d", config.IsProdEnv(), serverPort)
	engine.Run(":" + serverPort)
}
